<?php

namespace app\admin\controller;

use think\Loader;
use think\Request;
use app\common\controller\Base;
use app\common\model\ShopCat as ShopCatModel;

class ShopCat extends Base
{

    public function index(){
        $shopCat = ShopCatModel::all();

        return view('',['shopCat'=>$shopCat]);
    }

    public function add(){
        return view();
    }

    public function edit(Request $request){
        $id = $request->param('id',0,'intval');
        $shopCat = ShopCatModel::find($id);

        return view('',['shopCat'=>$shopCat]);
    }


    public function create(Request $request){
        if(!$request->isPost()){
            $this->error("非法渠道访问");
        }

        $data = $request->only(['name']);


        $validate = Loader::validate('ShopCatValidate');
        $result = $validate->scene('add')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }

        $shopCatModel = new ShopCatModel();
        $shopCatModel->save($data);

        $this->success('操作成功');
    }


    public function update(Request $request){
        if(!$request->isPost()){
            $this->error("非法渠道访问");
        }

        $data = $request->only(['name']);
        $id = $request->param('id',0,'intval');

        $validate = Loader::validate('ShopCatValidate');
        $result = $validate->scene('add')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }

        $shopCatModel = ShopCatModel::find($id);
        $shopCatModel->save($data);

        $this->success('操作成功');
    }


    public function delete(Request $request){
        if(!$request->isDelete()){
            $this->error("非法渠道访问");
        }
        $id = $request->param('id',0,'intval');
        ShopCatModel::destroy($id);
        $this->success('操作成功');
    }

}
